import fs from 'fs';
import { join } from 'path';
import { getDirname } from './utils';

const __dirname = getDirname(import.meta.url);

const core = (pkgName: string, typesUrl: string, mainUrl?: string) => {
  const pkgJsonPath = join(__dirname, '..', 'node_modules', pkgName, 'package.json');
  const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
  if (!pkgJson) return;
  if (pkgName === 'jsts') {
    pkgJson.main = mainUrl;
  } else {
    if (pkgJson.exports) {
      if (pkgJson.exports['.']) {
        pkgJson.exports['.'].types = `./${typesUrl}`;
      } else {
        pkgJson.exports.types = `./${typesUrl}`;
      }
    } else {
      pkgJson.types = typesUrl;
    }
  }
  fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
};

// core('jsts', '', 'dist/jsts.min.js');
core('@turf/turf', 'index.d.ts');
// core('@turf/meta', 'index.d.ts');
// core('@turf/helpers', 'dist/js/index.d.ts');
// core('@turf/invariant', 'dist/js/index.d.ts');
// core('@turf/center', 'dist/js/index.d.ts');
// core('@turf/projection', 'dist/js/index.d.ts');
// core('@turf/midpoint', 'index.d.ts');
